class RepairRecordsController < ApplicationController
  layout "backend"
  include PdfHelper
  def repair_list_partial
        if params[:list_name] == "rfp"
          @title = "Ready for Pickup"
          @records = RepairRecord.find(:all, :conditions => { :repair_state_id => "1" })
        elsif params[:list_name] == "awc"
          @title = "Awaiting Callback"
          @records =  RepairRecord.find(:all, :conditions => { :repair_state_id => "2" })
        elsif params[:list_name] == "open"
          @title = "Open Repairs"
          @records = RepairRecord.find(:all, :conditions => "repair_state_id = 3 or repair_state_id = 4 or repair_state_id = 5")
        elsif params[:list_name] == "awp"
          @title = "Awaiting Parts"
          @records = RepairRecord.find(:all, :conditions => { :repair_state_id => "6" })
        elsif params[:list_name] == "closed"
          @title = "Closed Repairs"
          @records = RepairRecord.find(:all, :conditions => "repair_state_id = 8 or repair_state_id = 9")
        elsif params[:list_name] == "awu"
          @title = "Awaiting Unit"
          @records = RepairRecord.find(:all, :conditions => { :repair_state_id => "10" })
      else
        @title = "All Records"
        @records = RepairRecord.find(:all)
      end
      render :partial => "partials/repair_list_partial", :locals => { :@title => @title, :@records => @records }
  end
  
  def authorization
    @repair_record = RepairRecord.find(params[:id])
    #render :layout => "backend"
  end
  def authorization_pdf
    authorization
    make_and_send_pdf('/repair_records/authorization', "R#{@repair_record.id.to_s}_auth.pdf")
  end
  def confirmation
    @repair_record = RepairRecord.find(params[:id])
    #render :layout => "backend"
  end
  def confirmation_pdf
    confirmation
    make_and_send_pdf('/repair_records/confirmation', "R#{@repair_record.id.to_s}_conf.pdf")
  end
  def index
    @records_rfp = RepairRecord.find(:all, :conditions => { :repair_state_id => "1" })
    @records_awc =  RepairRecord.find(:all, :conditions => { :repair_state_id => "2" })
    @records_open = RepairRecord.find(:all, :conditions => "repair_state_id = 3 or repair_state_id = 4 or repair_state_id = 5")
    @records_awp = RepairRecord.find(:all, :conditions => { :repair_state_id => "6" })
    @records_closed = RepairRecord.find(:all, :conditions => "repair_state_id = 8 or repair_state_id = 9")
    @records_awu = RepairRecord.find(:all, :conditions => { :repair_state_id => "10" })
    @records_all = RepairRecord.find(:all)
    if params[:list_name] == "rfp"
      @title = "Ready for Pickup"
      @records = RepairRecord.find(:all, :conditions => { :repair_state_id => "1" })
    elsif params[:list_name] == "awc"
      @title = "Awaiting Callback"
      @records =  RepairRecord.find(:all, :conditions => { :repair_state_id => "2" })
    elsif params[:list_name] == "open"
      @title = "Open Repairs"
      @records = RepairRecord.find(:all, :conditions => "repair_state_id = 3 or repair_state_id = 4 or repair_state_id = 5")
    elsif params[:list_name] == "awp"
      @title = "Awaiting Parts"
      @records = RepairRecord.find(:all, :conditions => { :repair_state_id => "6" })
    elsif params[:list_name] == "closed"
      @title = "Closed Repairs"
      @records = RepairRecord.find(:all, :conditions => "repair_state_id = 8 or repair_state_id = 9")
    elsif params[:list_name] == "awu"
      @title = "Awaiting Unit"
      @records = RepairRecord.find(:all, :conditions => { :repair_state_id => "10" })
    elsif params[:list_name] == "all"
      @title = "All Repairs"
      @records = RepairRecord.find(:all)
    else
      @title = "Open Records"
      @records = RepairRecord.find(:all, :conditions => "repair_state_id = 3 or repair_state_id = 4 or repair_state_id = 5")
    end
  end
  def show
    @repair_record = RepairRecord.find(params[:id])
  end
  def new_triage_note
    @repair_states = RepairState.find(:all)
    @triage_note = TriageNote.new
    @repair_record = RepairRecord.find(params[:id])
  end
  def create_triage_note
    @triage_note = TriageNote.new(params[:triage_note])
    @triage_note.user_id = current_user.id
    @triage_note.repair_record_id = params[:id]
    if @triage_note.save
      @repair_record = RepairRecord.find(params[:id])
      @repair_record.note_title =  @triage_note.note_title
      @repair_record.note =  @triage_note.note unless @triage_note.note.length == 0
      @repair_record.user_id = current_user.id
      @repair_record.assigned_to_id = @triage_note.assigned_to_id
      @repair_record.repair_state_id = @triage_note.repair_state_id
      if @repair_record.save
        if @repair_record.repair_state_id == 8 or @repair_record.repair_state_id == 9
          flash[:notice] = 'Repair note was successfully closed'
          redirect_to :action => "index", :list_name => "rfp"
        else
          flash[:notice] = 'Repair note was successfully created.'
          redirect_to :action => "edit", :id => params[:id]
        end
      end
    else
      flash[:notice] = 'Repair note creation failed.'
      render :action => "new" 
    end
  end
  def new
    @customer_records = CustomerRecord.find(:all, :order => 'lname')
    @cmodels = Cmodel.find(:all)
    @issues = Issue.find(:all)
    @repair_record = RepairRecord.new
  end
  def edit
    @repair_record = RepairRecord.find(params[:id])
    @triage_notes = TriageNote.find(:all, :conditions => [ "repair_record_id = ?", params[:id] ])
    if @triage_notes.length == 0
      @triage_note = TriageNote.new
      @triage_note.note_title = @repair_record.note_title
      @triage_note.note = @repair_record.note
      @triage_note.user_id = @repair_record.user_id
      @triage_note.repair_state_id = @repair_record.repair_state_id
      @triage_note.repair_record_id = @repair_record.id
      @triage_note.assigned_to_id = @repair_record.assigned_to_id
      @triage_note.save
      flash[:notice] = "Repair record was recreated with an initial triage note"
      redirect_to :action => "edit", :id => @repair_record.id
    end
    @users = User.find(:all)
    @cmodels = Cmodel.find(:all)
    @issues = Issue.find(:all)
    @repair_states = RepairState.find(:all)
    
  end
  def create
    @repair_record = RepairRecord.new(params[:repair_record])
    @repair_record.user_id = current_user.id
    @repair_record.repair_state_id = 3
    if @repair_record.save
      @triage_note = TriageNote.new
      @triage_note.note_title = params[:repair_record][:note_title]
      @triage_note.note = params[:repair_record][:note]
      @triage_note.user_id = current_user.id
      @triage_note.repair_state_id = 3
      @triage_note.repair_record_id = @repair_record.id
      if @triage_note.save
        flash[:notice] = 'Repair record was successfully created.'
        redirect_to :action => "edit", :id => @repair_record.id
      end
    else
      flash[:notice] = 'New record creation failed.'
      render :action => "new" 
    end
  end
  def update
    @repair_record = RepairRecord.find(params[:id])
    if @repair_record.update_attributes(params[:repair_record])
      flash[:notice] = 'Repair record was successfully updated.'
      redirect_to :action => "edit", :id => params[:id]
    else
      flash[:notice] = 'Repair record update failed.'
      render :action => "edit"
    end
  end
  def destroy
    @repair_record = RepairRecord.find(params[:id])
    @repair_record.destroy
  end
end
